package com.zprmb.udzt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zprmb.udzt.BaseActivity;
import com.zprmb.udzt.PayDexterityActivity;
import com.zprmb.udzt.R;
import com.zprmb.udzt.V6LoginActivity;
import com.zprmb.udzt.V6WebviewChild;
import com.zprmb.udzt.WebViewActivity;
import com.zprmb.udzt.bean.ZhiTouBean;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;

import org.json.JSONObject;

public class ZhiTouActivity extends BaseActivity implements View.OnClickListener {
    //标题头
    private TextView tv_title;
    //返回按钮
    private RelativeLayout rl_back;
//只投保的钱

    private EditText edit_zhitoumoney;

    String receivemoney;

    //可投资钱数

    private TextView text_keyongmoney;
    //可用余额


    //账号可用余额

    private TextView text_user_yue;
    //只投保的协议
    private TextView tv_mAgreement;

    String productId;

    //投资button

    private Button btn_invest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhitou);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("转入");
        rl_back = (RelativeLayout) findViewById(R.id.rl_back);
        edit_zhitoumoney = (EditText) findViewById(R.id.edit_zhitoumoney);
        receivemoney = String.valueOf(getIntent().getIntExtra("investamount", 0));
        edit_zhitoumoney.setText(receivemoney);
        text_keyongmoney = (TextView) findViewById(R.id.text_keyongmoney);
        //可用余额

//账户可用余额
        text_user_yue = (TextView) findViewById(R.id.text_user_yue);
        initLinstener();
        verityUser();
    }

    private void initLinstener() {
        rl_back.setOnClickListener(this);
        tv_mAgreement = (TextView) findViewById(R.id.tv_mAgreement);
        tv_mAgreement.setOnClickListener(this);
        //投资button
        btn_invest= (Button) findViewById(R.id.btn_invest);
        btn_invest.setOnClickListener(this);
    }

    private void verityUser() {
        VolleryUtil vollery = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                Log.d(jsonObject.toString(), "4444444444");
                if (jsonObject.optInt("success") == 1) {
                    Gson gson = new Gson();
                    ZhiTouBean zhiTouBean = new ZhiTouBean();
                    zhiTouBean = gson.fromJson(jsonObject.toString(), ZhiTouBean.class);
//                    text_keyongmoney
                    text_keyongmoney.setText(zhiTouBean.getData().getAvailableAmount());
                    //账号可用余额

                    text_user_yue.setText(zhiTouBean.getData().getUserFund().getAmount());


                    if (jsonObject.optInt("success") == 1) {
                        JSONObject json = jsonObject.optJSONObject("data");
                        Intent intent = null;


                        if (!json.optBoolean("isBindCard")) {
                            intent = new Intent(ZhiTouActivity.this,
                                    V6WebviewChild.class);
                            intent.putExtra("url", Constant.URL + "h5/account/bankcard/bind/view?app=true&token=");
                            intent.putExtra("title", "智投宝");
                            startActivity(intent);
                            return;
                        }

                    } else {
                        if (jsonObject.optString("comment").equals("please login")) {
                            Intent intent = new Intent(ZhiTouActivity.this,
                                    V6LoginActivity.class);
                            startActivity(intent);
                        } else {
                            ToastUtil.showCenterToast(ZhiTouActivity.this,
                                    jsonObject.optString("comment"));
                        }
                    }


                }

            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        vollery.setHeaders("token", getToken());
        vollery.setHeaders("client", "android");
        vollery.setShow(true);
        vollery.setRequestMethod(0);
        vollery.execute(ZhiTouActivity.this, Constant.URL
                + "/api/v5/user/userInfo", "");
    }

    /* if (!json.optBoolean("isBindCard")) {
           intent = new Intent(V6DexterityPayInActivity.this,
                   V6WebviewChild.class);
           intent.putExtra("url", Constant.URL + "h5/account/bankcard/bind/view?app=true&token=");
           intent.putExtra("title", "智投宝");
           startActivity(intent);
           return;
       }*/
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();

                break;

            case R.id.tv_mAgreement:
                Intent intent = new Intent(ZhiTouActivity.this,
                        WebViewActivity.class);
                intent.putExtra("url", "landing/currentAgreement?app=true&token=");
//                http://test1.udrmb.com/landing/currentAgreement?app=true&token=FhwMqLikXGoNvir5fiMoHKqlsztDe4cJsjoJbQ1asVHGdBJrQNjzoMDB0pgIHKo6XuAdWB5AYtc=

                intent.putExtra("share", 1);
                intent.putExtra("titleview", 1);
                intent.putExtra("title", "《智投宝授权委托书》");
                startActivity(intent);
                break;

            case R.id.btn_invest:
              Intent  intent2 = new Intent(ZhiTouActivity.this,
                        PayDexterityActivity.class);
                intent2.putExtra("type", "IN");
              /*  tv_v6_dexterity_pay_in_updata
                        .setBackgroundResource(R.drawable.btn_unclick);
                tv_v6_dexterity_pay_in_updata.setEnabled(false);
                tv_v6_dexterity_pay_in_updata
                        .setBackgroundResource(R.drawable.v6button);
                tv_v6_dexterity_pay_in_updata.setEnabled(true);*/
                intent2.putExtra("amount", edit_zhitoumoney.getText().toString() + "");
                intent2.putExtra("productId", productId);
                LogUtils.e("智投宝id", productId);
                startActivity(intent2);
                overridePendingTransition(R.anim.slide_in_from_bottom,
                        R.anim.slide_out_to_bottom);
                break;

        }
    }
}
